#include "qnetman2.h"

#include <QUrl>
#include <QtDeclarative>

QNetMan2* QNetMan2::instance = NULL;

QNetMan2::QNetMan2(QNMSettings *__qset) : QDeclarativeView(0), qset(__qset)
{
    cfgMonitor = NULL;
    netStat = NULL;
    qnmSettings = NULL;
    qnmtransl = NULL;
    cfgWiFiData = NULL;

    /* цвет темы приложения */
    QString theme = qset->getTheme();
    this->engine()->rootContext()->setContextProperty( "themeAppl", theme );

    /* ориентация страницы */
    QString orientation = qset->getOrientation();
    this->engine()->rootContext()->setContextProperty( "orientationAppl", orientation );

    qDebug() << "*** QNetMan2::QNetMan2()";
    QNetMan2::instance = this;
}

QNetMan2::~QNetMan2()
{
    if( cfgMonitor != NULL ) delete cfgMonitor;
    if( cfgWiFiData != NULL ) delete cfgWiFiData;
}

void QNetMan2::initQNetMan2()
{
    this->engine()->rootContext()->setContextProperty( "cppQNetMan", this );

    this->initMonitor();
    this->initNetStat();
    this->initSettings();
    this->initWiFiData();

    //this->setSource( QUrl("qml/QNetMan2/main.qml") );
    this->setSource( QUrl("/opt/QNetMan2/qml/QNetMan2/main.qml") );

}

void QNetMan2::initMonitor()
{
    if( cfgMonitor == NULL ) {
        cfgMonitor = new CfgMonitor( this, qset );
    }
    cfgMonitor->initListIfaces();
}

void QNetMan2::initNetStat()
{
    if( netStat == NULL ) {
        netStat = new NetStatExt( this );
    }
    this->engine()->rootContext()->setContextProperty("cppNetStat", netStat );
    this->initTranslation();
}

void QNetMan2::initSettings()
{
    if( qnmSettings == NULL ) {
        qnmSettings = new QNMOptions( qset, this );
    }
    this->engine()->rootContext()->setContextProperty("cppSettings", qnmSettings );
}

void QNetMan2::initTranslation()
{
    if( qnmtransl == NULL ) {
        qnmtransl = new QNMTranslation( this );
    }
    QString lang = qset->getLanguage();
    qnmtransl->selectLanguage( lang );
    this->engine()->rootContext()->setContextProperty("cppLanguage", qnmtransl);
}

void QNetMan2::initWiFiData()
{
    if( cfgWiFiData == NULL ) {
        cfgWiFiData = new WiFiListData( this, qset );
    }
    this->engine()->rootContext()->setContextProperty("cppWiFiData", cfgWiFiData );
    //cfgWiFiData->initListWiFiData();
}

void QNetMan2::quitQNetMan()
{
    qApp->quit();
}


QNetMan2* QNetMan2::getInstance()
{
    return QNetMan2::instance;
}

